package com.wmcsk.dl.dl.internal;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.os.Build;
import android.os.Bundle;

import com.wmcsk.dl.dl.DLPlugin;
import com.wmcsk.dl.dl.utils.DL;
import com.wmcsk.dl.dl.utils.DLConfigs;
import com.wmcsk.dl.dl.utils.DLConstants;

import java.lang.reflect.Constructor;


/**
 * This is a plugin activity proxy, the proxy will create the plugin activity
 * with reflect, and then call the plugin activity's attach、onCreate method, at
 * this time, the plugin activity is running.
 */
public class DLProxyImpl {

	private static final String TAG = DLProxyImpl.class.getName();
	private String mClass;
	private String mPackageName;

	private DLPluginPackage mPluginPackage;
	private DLPluginManager mPluginManager;

	private AssetManager mAssetManager;
	private Resources mResources;
	private Theme mTheme;

	private ActivityInfo mActivityInfo;
	private Activity mProxyActivity;
	protected DLPlugin mPluginActivity;
	public ClassLoader mPluginClassLoader;

	public DLProxyImpl(Activity activity) {
		mProxyActivity = activity;
	}

	private void initializeActivityInfo() {
		PackageInfo packageInfo = mPluginPackage.packageInfo;
		if ((packageInfo.activities != null)
				&& (packageInfo.activities.length > 0)) {
			if (mClass == null) {
				mClass = packageInfo.activities[0].name;
			}
			// Finals 修复主题BUG
			int defaultTheme = packageInfo.applicationInfo.theme;
			for (ActivityInfo a : packageInfo.activities) {
				if (a.name.equals(mClass)) {
					mActivityInfo = a;
					// Finals ADD 修复主题没有配置的时候插件异常
					if (mActivityInfo.theme == 0) {
						if (defaultTheme != 0) {
							mActivityInfo.theme = defaultTheme;
						} else {
							if (Build.VERSION.SDK_INT >= 14) {
								mActivityInfo.theme = android.R.style.Theme_DeviceDefault;
							} else {
								mActivityInfo.theme = android.R.style.Theme;
							}
						}
					}
				}
			}

		}
	}

	private void handleActivityInfo() {
		if (mActivityInfo.theme > 0) {
			mProxyActivity.setTheme(mActivityInfo.theme);
		}
		Theme superTheme = mProxyActivity.getTheme();
		mTheme = mResources.newTheme();
		mTheme.setTo(superTheme);
		// Finals适配三星以及部分加载XML出现异常BUG
		try {
			mTheme.applyStyle(mActivityInfo.theme, true);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// TODO: handle mActivityInfo.launchMode here in the future.
	}

	public void onCreate(Intent intent){

		// set the extra's class loader
		intent.setExtrasClassLoader(DLConfigs.sPluginClassloader);

		mPackageName = intent.getStringExtra(DLConstants.EXTRA_PACKAGE);
		mClass = intent.getStringExtra(DLConstants.EXTRA_CLASS);

		mPluginManager = DLPluginManager.getInstance(mProxyActivity);
		mPluginPackage = mPluginManager.getPackage(mPackageName);
		mAssetManager = mPluginPackage.assetManager;
		mResources = mPluginPackage.resources;

		initializeActivityInfo();
		handleActivityInfo();
		launchTargetActivity();
	}

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	protected void launchTargetActivity(){
		try{
			Class<?> localClass = getClassLoader().loadClass(mClass);
			Constructor<?> localConstructor = localClass
					.getConstructor(new Class[] {});
			Object instance = localConstructor.newInstance(new Object[] {});
			mPluginActivity = (DLPlugin) instance;
			((DLAttachable) mProxyActivity).attach(mPluginActivity, mPluginManager);
			DL.d(TAG, "instance = " + instance);
			// attach the proxy activity and plugin package to the mPluginActivity
			mPluginActivity.attach(mProxyActivity, mPluginPackage);

			Bundle bundle = new Bundle();
			bundle.putInt(DLConstants.FROM, DLConstants.FROM_EXTERNAL);
			// 调用插件的onCreate方法
			mPluginActivity.onCreate(bundle);
		}catch (Exception e){
			e.printStackTrace();
		}

	}

	public ClassLoader getClassLoader() {
		return mPluginPackage.classLoader;
	}

	public AssetManager getAssets() {
		return mAssetManager;
	}

	public Resources getResources() {
		return mResources;
	}

	public Theme getTheme() {
		return mTheme;
	}

	public DLPlugin getRemoteActivity() {
		return mPluginActivity;
	}
}
